using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer.Base;
using SqlCeViewer.Utility;

namespace SqlCeViewer.DynaControls
{
    public partial class DbOpenedHistory : BaseSubForm
    {
        public delegate void Del_AfterSelectedValue(string a);
        public Del_AfterSelectedValue AfterSelectedValue = null;

        public DbOpenedHistory()
        {
            InitializeComponent();


            dbHistoryWin.KeyDown += new KeyEventHandler(dbHistoryWin_KeyDown);
            dbHistoryWin.DoubleClick += new EventHandler(dbHistoryWin_DoubleClick);
            
            dbHistoryWin.Items.Clear();
            List<string> list = SerializeClass.GetHistoryList();
            if (list != null)
            {
                foreach (string item in list)
                {
                    dbHistoryWin.Items.Add(item);
                }
            }

        }

        void dbHistoryWin_DoubleClick(object sender, EventArgs e)
        {
            DoSelect();
        }


        void dbHistoryWin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DoSelect();
            }

        }

        private void DoSelect()
        {
            string result = string.Empty;

            if (this.dbHistoryWin.SelectedItem != null)
            {
                result = this.dbHistoryWin.SelectedItem.ToString();
                if (AfterSelectedValue != null)
                {
                    AfterSelectedValue(result);

                }
                Close();
            }
        }

    }
}